/**
 * 
 */
package com.jm.web.data;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.jm.utils.Token;
import com.jm.web.BaseMainPage;
import com.jm.web.BasePage;

/**
 * @author yanjie
 *
 * 2011/9/28 上午9:49:17
 */
public class JMForm extends BaseDataEntity{
	protected boolean bBind = false ;
	protected HttpServletRequest request = null ;
	protected String strAction = "" ;
	protected String strActionValue = "" ;
	protected String strOldAction = "" ;
	protected String strOldActionValue = "" ;
	protected String strPageType = "" ;
	protected String strToken = "" ;
	
	public boolean isBind() {
		return bBind;
	}

	/**
	 * 通过上下文对象填充实体
	 * @param webContext
	 * @param searchCallConfig
	 * @return
	 */
	public void BindForm(HttpServletRequest request) {
		if(this.bBind) return ;
		
		this.bBind = true ;
		this.request = request ;
		
		String contentType = request.getContentType();
		 
		if ((contentType == null) || (!(contentType.toLowerCase().startsWith("multipart/form-data"))))
			BindNormalRequestForm(request);
		else
			BindMultipartRequestForm(request);
		
		this.strAction = this.GetParamStringValue("action", "");
		this.strActionValue = this.GetParamStringValue("actionvalue", "");
		this.strOldAction = this.GetParamStringValue("oldaction", "");
		this.strOldActionValue = this.GetParamStringValue("oldactionvalue", "");
		this.strPageType = this.GetParamStringValue("pagetype", "");
		this.strToken = this.GetParamStringValue(Token.TOKEN_STRING_NAME, "");
	}
	
	public void resetAction(){
		this.strAction = "";
		this.strActionValue = "";
		this.strOldAction = "";
		this.strOldActionValue = "";
		this.strPageType = "";
		this.strToken = "";
	}
	
	/**
	 * @param request
	 * 正常表单数据绑定
	 */
	protected void BindNormalRequestForm(HttpServletRequest request){
		Enumeration en = request.getParameterNames();
		while (en.hasMoreElements()) {
			 String name = (String)en.nextElement();
			 String[] values = request.getParameterValues(name);
			 if(name.endsWith("[]")){
				name = name.substring(0,name.indexOf("[]"));
			 }
			 if (ArrayUtils.isEmpty(values)) {
				 this.SetParamValue(name, null);
			 }else if (values.length > 1 || this.getPage().getItemSize(name,true) > 1) {
				 this.SetParamValue(name, values);
			 }
			 else {
				 this.SetParamValue(name, values[0]);
			 }
		 }
	}
	
	/**
	 * @param request
	 * 二进制表单数据绑定
	 */
	protected void BindMultipartRequestForm(HttpServletRequest request){
		CommonsMultipartResolver cm = new CommonsMultipartResolver(request.getSession().getServletContext());
		cm.setDefaultEncoding("UTF-8");
		MultipartHttpServletRequest  multipartReq = cm.resolveMultipart(request);
		
		//处理二进制文件
		Iterator it = multipartReq.getFileNames();
		if(it != null)
		{
			while (it.hasNext()) {
				String name = (String)it.next();
				MultipartFile multipartFile = multipartReq.getFile(name);
				if(multipartFile != null)
				{
					//System.out.println(name);
					if(name.indexOf("_array") >=0 ){
						name = name.substring(0,name.indexOf("_array"));
					}
					
					if(!this.ContainesParam(name)){
						this.SetParamValue(name, multipartFile);
					}else{
						Object object = this.GetParamValue(name);
						if(object instanceof MultipartFile){
							MultipartFile files[] = new MultipartFile[2];
							files[0] = (MultipartFile)object;
							files[1] = multipartFile;
							this.SetParamValue(name, files);
						}else if(object instanceof MultipartFile[]){
							MultipartFile files[] = (MultipartFile[])object;
							List<MultipartFile> list = new ArrayList<MultipartFile>();
							for (int i = 0; i < files.length; i++) {
								list.add(files[i]);
							}
							files = new MultipartFile[list.size()];
							list.add(multipartFile);
							list.toArray(files);
							this.SetParamValue(name, files);
						}
					}
				}
			}
		}
		
		//处理其它参数
		Enumeration en = multipartReq.getParameterNames();
		while (en.hasMoreElements()) {
			 String name = (String)en.nextElement();
			 String[] values = multipartReq.getParameterValues(name);
			 
			 if (ArrayUtils.isEmpty(values)) {
				 this.SetParamValue(name, null);
			 }
			 
			 else if (values.length > 1 || this.getPage().getItemSize(name,true) > 1) {
				 this.SetParamValue(name, values);
			 }
			 else {
				 this.SetParamValue(name, values[0]);
			 }
		 }
	}
	
	public BaseMainPage getPage(){
		return (BaseMainPage)this.request.getAttribute(BasePage.TAG_CURRENT_PAGE);
	}
	
	public String getAction() {
		return strAction;
	}

	public String getActionValue() {
		return strActionValue;
	}

	public String getPageType() {
		return strPageType;
	}

	public String getToken() {
		return strToken;
	}

	public String getOldAction() {
		return strOldAction;
	}

	

	public String getOldActionValue() {
		return strOldActionValue;
	}
	
	public void setOldAction(String strOldAction) {
		this.strOldAction = strOldAction;
		this.SetParamValue("oldaction",strOldAction);
	}
	
	public void setOldActionValue(String strOldActionValue) {
		this.strOldActionValue = strOldActionValue;
		this.SetParamValue("oldactionvalue",strOldActionValue);
	}

	public void setAction(String strAction) {
		this.strAction = strAction;
		this.SetParamValue("action",strAction);
	}

	public void setActionValue(String strActionValue) {
		this.strActionValue = strActionValue;
		this.SetParamValue("actionvalue",strActionValue);
	}
	
	public Map<String ,BaseDataEntity> getListItemRecordMap(String name){
		return this.getPage().getListItemRecordMap(name);
	}
	
	public BaseDataEntity getListItemRecord(String name,String value){
		return this.getPage().getListItemRecord(name,value);
	}
	
	public String getListItemRecordAttributeValue(String name,String key){
		return this.getListItemRecordAttributeValue(name, key, this.gpsv(name));
	}
	
	public String getListItemRecordAttributeValue(String name,String key,String value){
		return this.getPage().getListItemRecordAttributeValue(name,key,value);
	}
	
	public String getWFInstance(){
		return this.gpsv("wfinstance");
	}
	
	public String getWFDataKeyValue(){
		return this.gpsv("wfdatakeyvalue");
	}
	
	public boolean ationEq(String params){
		return (","+params+",").indexOf(","+this.getAction()+",") >= 0;
	}
	
	public boolean ationValueEq(String params){
		return (","+params+",").indexOf(","+this.getActionValue()+",") >= 0;
	}
}
